package com.jasonchoi.security.commons.base;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @Author: JasonChoi
 * @Date: 2020/1/8 10:08
 */
@Data
@Accessors(chain = true)
public class BaseRestResult implements Serializable {
    public static final Integer OK = 200;
    public static final Integer ERROR = 9999;
    public static final Integer NEED_LOGIN = 443;

    private Integer status;
    private String message;

    public BaseRestResult() {
    }

    private static class SingletonHolder{
        private static final BaseRestResult instance = new BaseRestResult();
    }

    public static  BaseRestResult bulild(){
        return SingletonHolder.instance;
    }

    public static BaseRestResult ok(){
        return bulild().setStatus(OK);
    }

    public static BaseRestResult error(){
        return bulild().setStatus(ERROR);
    }

    public static BaseRestResult needLogin(){
        return bulild().setStatus(NEED_LOGIN);
    }

    public static BaseRestResult ok(String message){
        return BaseRestResult.ok().setMessage(message);
    }

    public static BaseRestResult error(String message){
        return BaseRestResult.error().setMessage(message);
    }

    public String toJson(){
        return JSONObject.toJSONString(this);
    }

}
